package com.fruit.service.order.impl;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.fruit.mapper.order.OrderMapper;
import com.fruit.pojo.order.OrderException;
import com.fruit.pojo.order.TransactionException;
import com.fruit.util.DateUtil;

import lombok.extern.slf4j.Slf4j;


/**
 * 
 * 订单日志类
 *
 * @author shibeilei
 * @version 2017.12.01 11:10
 *
 */
@Slf4j
@Service
public class OrderLogService {
	
	@Autowired
	private OrderMapper orderMapper;
	
	/**
	 * 插入交易流水日志
	 * @param tradeId tradeId
	 * @param serviceName serviceName
	 * @param serviceState serviceState
	 * @param transactionCode transactionCode
	 * @param userId userId
	 * @param code code
	 * @param message message
	 * @author shibeilei
	 * @version 2017.12.01 10:14
	 */
	public void newSaveExceptionTransaction(String tradeId, String serviceName, String serviceState,
			String transactionCode, int userId, String code, String message) {
		try {
			log.info("[{}]-【交易流水异常日志】-【交易流水号：{}】", tradeId, transactionCode);
			TransactionException te = new TransactionException();
			te.setTradeId(tradeId);
			te.setServiceName(serviceName);
			te.setTransactionCode(transactionCode);
			te.setUserId(userId);
			JSONObject paJson = new JSONObject();
			paJson.put("code", transactionCode);
			te.setInputParams(paJson.toString());
			te.setServiceState(serviceState);
			te.setErrorCode(code);
			te.setErrorMessage(message);
			te.setQueryTime(DateUtil.getTime());
			orderMapper.saveExceptionTransaction(te);
			log.info("[{}]-【交易流水异常日志】-【成功】", tradeId);
		} catch (Exception e) {
			log.error("[{}]-【交易流水异常日志】-【异常】------【{}】", tradeId, getStackTraceAsString(e));
		}
		log.info("[{}]-【交易流水异常日志】-【结束】", tradeId);

	}

	/**
	 * 插入订单异常日志
	 * @param tradeId tradeId
	 * @param serviceName serviceName
	 * @param serviceState serviceState
	 * @param orderCode orderCode
	 * @param userId userId
	 * @param code code
	 * @param message message
	 * @author shibeilei
	 * @version 2017.12.01 11:10
	 */
	public void newSaveExceptionOrder(String tradeId, String serviceName, String serviceState, String orderCode,
			int userId, String code, String message) {
		try {
			log.info("[{}]-【订单异常日志】-【订单号：{}】", tradeId, orderCode);
			OrderException oe = new OrderException();
			oe.setTradeId(tradeId);
			oe.setServiceName(serviceName);
			oe.setOrderCode(orderCode);
			oe.setUserId(userId);
			JSONObject paJson = new JSONObject();
			paJson.put("code", orderCode);
			oe.setInputParams(paJson.toString());
			oe.setServiceState(serviceState);
			oe.setErrorCode(code);
			oe.setErrorMessage(message);
			oe.setQueryTime(DateUtil.getTime());
			orderMapper.saveExceptionOrder(oe);
			log.info("[{}]-【订单异常日志】-【成功】", tradeId);
		} catch (Exception e) {
			log.error("[{}]-【订单异常日志】-【异常】------【{}】", tradeId, getStackTraceAsString(e));
		}
		log.info("[{}]-【订单异常日志】-【结束】", tradeId);

	}

	/**
	 * 打印异常信息
	 * @param ex ex
	 * @return 返回异常信息
	 * @author shibeilei
	 * @version 2017.12.01 11:10
	 */
	public String getStackTraceAsString(Throwable ex) {
		StringWriter stringWriter = new StringWriter();
		ex.printStackTrace(new PrintWriter(stringWriter));
		return stringWriter.toString();
	}

}
